package policy

type tSelectSortPolicy struct {
}

func NewSelectSortPolicy() ISortPolicy {
	return &tSelectSortPolicy{}
}

func (self *tSelectSortPolicy) Name() string {
	return "SelectSort"
}

func (self *tSelectSortPolicy) Sort(data []int) []int {
	if data == nil {
		return nil
	}

	size := len(data)
	if size <= 1 {
		return data
	}

	i := 0
	for {
		if i >= size - 1 {
			break
		}

		p, m := self.min(data, i + 1, size - 1)
		if m < data[i] {
			data[p], data[i] = data[i], data[p]
		}

		i++
	}

	return data
}

func (self *tSelectSortPolicy) min(data []int, from int, to int) (p int, v int) {
	i := from
	p = from
	v = data[from]

	if to <= from {
		return p, v
	}

	for {
		i++
		if i > to {
			break
		}

		if data[i] < v {
			p = i
			v = data[i]
		}
	}

	return p, v
}
